package com.itheima.app.controller;

import com.itheima.app.manager.UserManager;
import com.itheima.domain.db.User;
import com.itheima.domain.db.UserInfo;
import com.itheima.service.db.UserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
public class UserController {
    @Autowired
    private UserManager userManager;

    /*
    用户保存
     */
    @PostMapping("/user/save")
    public ResponseEntity save(@RequestBody User user) {
        return userManager.save(user);
    }

    /*
    通过手机号查找用户
     */
    @GetMapping("/user/findByPhone")
    public ResponseEntity findByPhone(String phone) {
        return userManager.findByPhone(phone);
    }

    /*
    发送验证码
     */
    @PostMapping("/user/login")
    public void sendSms(@RequestBody Map<String, String> map) {
        userManager.sendSms(map.get("phone"));
    }

    /*
    注册和登录功能
     */
    @PostMapping("/user/loginVerification")
    public ResponseEntity regAndLogin(@RequestBody Map<String, String> map) {
        //获取请求信息
        String phone = map.get("phone");
        String verificationCode = map.get("verificationCode");
        //调用manager完成功能
        return userManager.regAndLogin(phone, verificationCode);
    }
    //保存新用户基本信息
    @PostMapping("/user/loginReginfo")
    public  void saveUserInfoBase(@RequestBody UserInfo userInfo,@RequestHeader("Authorization") String token){
        //调用manager实现功能
        userManager.saveUserInfoBase(userInfo,token);
    }
    //头像上传和头像修改功能
    @PostMapping({"/user/loginReginfo/head","/users/header"})
    public ResponseEntity saveUserInfoHead(MultipartFile headPhoto,@RequestHeader("Authorization") String token) throws IOException {
        return userManager.saveUserInfoHead(headPhoto,token);
    }
    //查询用户“我的”页面信息
    @GetMapping("/users")
    public ResponseEntity findUserInfoVo(@RequestHeader("Authorization") String token,Long userID,Long huanxinID){
        /*
        通过页面参数查询信息
         */
        if (userID!=null){
        return   userManager.findUserInfoVo(userID);
        }else if (huanxinID!=null){
          return   userManager.findUserInfoVo(huanxinID);
        }else {
            User user = userManager.findUserByToken(token);
           return userManager.findUserInfoVo(user.getId());
        }
    }
    //修改用户“我的”页面信息
    @PutMapping("/users")
    public void updateUserInfoBase(@RequestBody UserInfo userInfo){
      userManager.updateUserInfoBase(userInfo);
    }
}
